home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Frameworks / Grant's CGI Framework 1.0b12 / Util / Quit.c < prev    next >
Text File  |  1995-12-09  |  3KB  |  145 lines

  1. /*****
  2.  *
  3.  *    Quit.c
  4.  *
  5.  *    This is a support file for "Grant's CGI Framework".
  6.  *    Please see the license agreement that accompanies the distribution package
  7.  *    for licensing details.
  8.  *
  9.  *    Copyright ©1995 by Grant Neufeld
  10.  *    grant@acm.com
  11.  *    http://arpp1.carleton.ca/grant/
  12.  *
  13.  *****/
  14.  
  15. #include "MyConfiguration.h"
  16.  
  17. #include "compiler_stuff.h"
  18. #include "globals.h"
  19.  
  20. #include "EventUtil.h"
  21. #include "MemoryUtil.h"
  22. #include "ProcessUtil.h"
  23. #include "WindowInt.h"
  24.  
  25. #include "Quit.h"
  26.  
  27.  
  28. /***  INTERFACE  ***/
  29.  
  30. /* Handle an interface or scripting call to quit the applciation */
  31. void
  32. doQuitApp ( void )
  33. {
  34.     /* set application to quit */
  35.     gQuit = true;
  36. } /* doQuitApp */
  37.  
  38.  
  39. /***  FUNCTIONS  ***/
  40. #pragma mark -
  41.  
  42. /* Confirm that the application can quit, prompt the user if necessary.
  43.     Clean up, if the application can quit.
  44.     Return true if application is set to quit, false if it can't quit. */
  45. Boolean
  46. QuitPrepare ( Boolean allowUserInteract )
  47. {
  48.     Boolean            memoryLow;
  49.     Boolean            gotEvent;
  50.     EventRecord        theEvent;
  51.     
  52.     #if kCompileWithForeground
  53.     Boolean            success;
  54.     #endif
  55.     
  56.     gQuit = true;
  57.     
  58.     /* check for emergency memory store. If it isn't available, memory is low */
  59.     memoryLow = !IsEmergencyMemAvail ();
  60.     
  61.     /* Should add check for outstanding events and handle them as appropriate.
  62.         Especially important is to check for any queued high level events. */
  63.     if ( !memoryLow )
  64.     {
  65.         /* check for outstanding events and handle them as appropriate */
  66.         do
  67.         {
  68.             gotEvent = WaitNextEvent ( highLevelEventMask, &theEvent, 0, nil );
  69.             
  70.             if ( gotEvent )
  71.             {
  72.                 /* Need to figure out a good way to distinguish between idle time
  73.                     quits and others. So, if quitting on idle and an event is
  74.                     found gQuit will be reset to false. Otherwise, if quitting
  75.                     normally and an event is found gQuit will be left alone. */
  76.                 
  77.                 switch ( theEvent.what )
  78.                 {
  79.                     case kHighLevelEvent :
  80.                         doHighLevelEvent ( &theEvent );
  81.                         break;
  82.                 }
  83.             }
  84.         } while ( gotEvent == true );
  85.     }
  86.         
  87.     #if kCompileWithForeground
  88.     /* get rid of document windows */
  89.     success = WindowCloseAll ( (!memoryLow) && allowUserInteract );
  90.     
  91.     if ( success || !allowUserInteract || memoryLow )
  92.     #endif    /* kCompileWithForeground */
  93.     {
  94.         #if kCompileWithThreadsOptional
  95.         if ( gHasThreadMgr )
  96.         #endif
  97.         {
  98.             /* while there remain other threads, let them finish before quitting */
  99.             ThreadFinishAllSubThreads ();
  100.         }
  101.         
  102.         success = MyQuit ( allowUserInteract );
  103.         
  104.         /* can quit if successful or user is not allowed to interact */
  105.         gQuit = ( success || !allowUserInteract || memoryLow );
  106.     }
  107.     #if kCompileWithForeground
  108.     else
  109.     {
  110.         gQuit = false;
  111.     }
  112.     #endif
  113.     
  114.     return gQuit;
  115. } /* QuitPrepare */
  116.  
  117.  
  118. /* force the application to quit, doing necessary cleanup. */
  119. void
  120. ForceQuit ( void )
  121. {
  122.     /* quit with no user interaction */
  123.     QuitPrepare ( false );
  124.     
  125.     ExitToShell ();
  126. } /* ForceQuit */
  127.  
  128.  
  129. /**  Utilities  **/
  130. #pragma mark -
  131.  
  132. #if kCompileWithQuitOnLongIdle
  133. void
  134. ResetQuitIdleTimer ( void )
  135. {
  136.     if ( gDoIdleQuit )
  137.     {
  138.         gTimeLastAction = TickCount ();
  139.     }
  140. } /* ResetQuitIdleTimer */
  141. #endif
  142.  
  143.  
  144. /*****  EOF  *****/
  145.